package com.example.lotterysystem.common.interceptor;

import com.example.lotterysystem.common.util.JWTUtil;
import io.jsonwebtoken.Claims;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.swing.*;

@Slf4j
@Component
public class LoginInterceptor implements HandlerInterceptor {
    //拦截器

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //获取请求头
        String str = request.getHeader("user_token");
        log.info("获取token: "+str);
        log.info("获取路径: {}",request.getRequestURI());
        //令牌解析
        Claims claims = JWTUtil.parseJWT(str);
        if(claims==null){
            log.error("解析令牌失败");
            response.setStatus(401);
            return false;
        }
        log.info("解析成功");
        return true;
    }
}
